package com.huixue.cn.ztej.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.Section;
import com.huixue.cn.ztej.bean.User;
import com.huixue.cn.ztej.dao.UserMapper;
import com.huixue.cn.ztej.enums.RoleStatus;
import com.huixue.cn.ztej.service.SysProjectService;
import com.huixue.cn.ztej.service.SysSectionService;
import com.huixue.cn.ztej.service.SysUserService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.annotation.RequestMappingName;
import com.huixue.cn.ztej.utils.framwork.JacksonUtil;
import com.huixue.cn.ztej.utils.framwork.PageParameter;

@Controller
@RequestMapping(value = "/sys/sysProjectMan")
public class SectionController extends BaseController {
	private static Logger logger = LoggerFactory
			.getLogger(SectionController.class);
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired(required = true)
	SysUserService sysUserService;
	
	@Autowired(required = true)
	SysSectionService sysSectionService;
	
	@Autowired(required = true)
	SysProjectService sysProjectService;

	/**
	 * <p>跳转到列表页面</p>
	 */
	@RequestMappingName(value = "跳转到列表页面")
	@RequestMapping(value = "SectionInfo/toSearch", method = RequestMethod.GET)
	public String toSearch(@RequestParam(value = "segCode", defaultValue = "") String segCode,@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = PageParameter.DEFAULT_PAGE_SIZE+"") Integer rows,
			HttpServletRequest request,HttpServletResponse response,Model model) {
		
		Map<String, Object> searchParams = new HashMap<String,Object>();
		searchParams.put("segCode", segCode.trim());
		searchParams.put("userName", request.getSession().getAttribute("loginAccount"));
		
		User currentUser = (User)request.getSession().getAttribute("loginUser");

		//项目管理
		if(currentUser.getRoleId()!=null && currentUser.getRoleId().equals(RoleStatus.ROLE_8.getRoleId()+"")){
			searchParams.put("proId", currentUser.getProjectID());
		}else{
			Results projectRes =  sysProjectService.findAll(searchParams);
			model.addAttribute("projectRes", projectRes);
		}
		
		String proejctId = request.getParameter("projectId");
		if(proejctId!=null && proejctId.trim().length()>0 && Integer.valueOf(proejctId) >0 ){
			searchParams.put("proId", proejctId);
			model.addAttribute("projectId", proejctId);
		}
		
		Results results = sysSectionService.searchByPage(searchParams,page,rows);
	
		model.addAttribute("res", results);

		return "/com/ztej/project/sysSecInfo";
	}
	
	
	/**
	 * <p>跳转到添加页面</p>
	 */
	@RequestMappingName(value = "跳转到添加页面")
	@RequestMapping(value = "SectionInfo/toAdd", method = RequestMethod.GET)
	public String toAdd(Model model,HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> searchParams = new HashMap<String,Object>();
		User currentUser = (User)request.getSession().getAttribute("loginUser");

		//项目管理
		if(currentUser.getRoleId()!=null && currentUser.getRoleId().equals(RoleStatus.ROLE_8.getRoleId()+"")){
			searchParams.put("proId", currentUser.getProjectID());
		}
		
        Results projects = sysProjectService.findAll(searchParams);
		model.addAttribute("projects", projects);
		return "/com/ztej/project/sysSecInfoAdd";
	}
	
	/**
	 * <p>根据项目查找工段</p>
	 */
	@RequestMappingName(value = "根据项目查找工段")
	@RequestMapping(value = "SectionInfo/sectionByProId/{proId}", method = RequestMethod.POST,consumes="application/json")
	@ResponseBody
	public Results sectionByProId(@PathVariable("proId")Integer proId,HttpServletRequest request) {
		Results results = null;
		List<Section> list  = sysSectionService.listSectionByPro(proId);
		System.out.println("list==="+list.size());
		results = new Results(API_RETURN_STATUS.NORMAL.value(), "工段信息保存成功！",list);
		return results;
	}
	
	
	/**
	 * <p>增加项目基本信息</p>
	 */
	@RequestMappingName(value = "增加用户基本信息")
	@RequestMapping(value = "SectionInfo/add", method = RequestMethod.POST,consumes="application/json")
	@ResponseBody
	public Results add(@RequestBody Section sysSecInfo,HttpServletRequest request) {
		Results results = null;
		results = sysSectionService.add(sysSecInfo);
		logger.info(new JacksonUtil().getJson(results));
		return results;
	}
	
	/**
	 * <p>跳转到添加页面</p>
	 */
	@RequestMappingName(value = "跳转到更新页面")
	@RequestMapping(value = "SectionInfo/toModify/{secId}", method = RequestMethod.GET)
	public String toModify(@PathVariable("secId")Integer secId,Model model,HttpServletRequest request) {
		Results results = sysSectionService.findBySecId(secId);
		
		Map<String, Object> searchParams = new HashMap<String,Object>();
		User currentUser = (User)request.getSession().getAttribute("loginUser");

		//项目管理
		if(currentUser.getRoleId()!=null && currentUser.getRoleId().equals(RoleStatus.ROLE_8.getRoleId()+"")){
			searchParams.put("proId", currentUser.getProjectID());
		}
		
        Results projects = sysProjectService.findAll(searchParams);
		
		model.addAttribute("projects", projects);
		model.addAttribute("sysSecInfo", results.getData());
		return "/com/ztej/project/sysSecInfoModify";
		
	}
	
	/**
	 * <p>增加项目基本信息</p>
	 */
	@RequestMappingName(value = "修改用户基本信息")
	@RequestMapping(value = "SectionInfo/modify", method = RequestMethod.POST)
	@ResponseBody
	public Results modify(@RequestBody Section sysSecInfo,HttpServletRequest request) {
		
		Results results = null;
		
		results = sysSectionService.modify(sysSecInfo);
		
		logger.info(new JacksonUtil().getJson(results));
		
		return results;
	}
	
	/**
	 * <p>删除工段基本信息</p>
	 * 
	 */
	@RequestMappingName(value = "删除工段基本信息")
	@RequestMapping(value = "SectionInfo/delete/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public Results delete(@PathVariable("id")Integer id,HttpServletRequest request) {
		
		Results results = null;
		try {
			
			
			results = sysSectionService.delete(id);
			
		} catch (Exception e) {
			results = new Results(
					API_RETURN_STATUS.SERVER_INTERNAL_ERROR.value(),
					API_RETURN_STATUS.SERVER_INTERNAL_ERROR.desc());
		}
		logger.info(new JacksonUtil().getJson(results));
		return results;
		
	}
	
	

}
